<?php
namespace app\index\controller;

//使用时需要use一下
use think\Request;
use think\Db;
use think\Controller;
use think\Session;
use app\index\model\Goods;


class Index extends Controller
{
	//测试数据库操作
	public function database()
	{
		//增加
		//Db::execute('insert into tp_data(name) values("dagang")');
		//修改
		//Db::execute('update tp_data set status=1 where id=3');
		//删除
		//Db::execute('delete from tp_data where id=4');
		//查询
		dump(Db::query('select * from tp_data'));

		//链式操作
		//dump(Db::table('tp_data')->where('id=5')->select());
		//dump(Db::name('data')->where('id=5')->select());
		//dump(Db::name('data')->where('id>2')->find());
		//dump(Db::name('data')->where('id','>',2)->select());
		//dump(Db::name('data')->where('id','>=',0)->select());
		//dump(Db::name('data')->where('id','=',1)->select(false));
		/*
		dump(Db::name('data')
				->where([
							'id' => ['between','1,3'],
							'name'=> ['like','%xiao%']
						])
				->buildSql());
		dump(Db::name('data')->select(function($query){
			$query->where('id>3')->order('id desc');
		}));
		dump(Db::name('data')->where('id=1')->value('name'));
		dump(Db::name('data')
				->where('id>0')
				->column('id,name'));
		dump(Db::name('data')
				->whereTime('create_time','>','2017-1-7')
				->select());
			dump(Db::name('data')->whereTime('create_time','>','-2 days')->select());
		Db::name('data')->chunk(2,function($list){
			foreach ($list as $value) {
				dump($value['name']);
				if ($value['name'] == 'doudou') {
					return false;
				}
			}
			echo '=====';
		});*/
	Db::name('data')->chunk(2,function($list){
		foreach ($list as $value){
			dump($value['name']);
			if($value['name'] == 'jingjing'){
				return false;
			}
		}
		echo '============';
	});
	dump(Db::name('data')->field('id,name')->where('id','>',3)->fetchSql()->delete());
}
	//3、依赖注入
	public function qingqiu(Request $request)
	{
		//测试请求
		//1、传统方式，是单例对象
		//$request = Request::instance();
		//2、助手函数
		//$request = request();
		//获取URL
		//return $request->url(true);
		//return $request->domain();
		//return $request->ext();
		//return $request->module();
		//return $request->ip();
		//dump($request->param());
		//dump($request->only(['username']));
		//dump($request->except(['username']));
		//dump($request->has('username','get'));
		//dump(input('?get.id'));
		dump($request->get('username','abc','strtoupper'));
	}

	public function test($a,$b)
	{
		return $a + $b;
	}

    public function index(Goods $goods)
    {
    	$uid=Session::get('uid');
    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
    	$dt=$goods->where(['classify'=>66])->select();
    	$dtt=$goods->where(['classify'=>65])->select();
    	$this->assign('dtt',$dtt);
    	$this->assign('dt',$dt);
    	$good=$goods->where(['classify'=>1,'isfall'=>1])->select();
    	$gooda=$goods->where(['classify'=>2,'isfall'=>1])->select();
    	$goodb=$goods->where(['classify'=>3,'isfall'=>1])->select();
    	$goodc=$goods->where(['classify'=>4,'isfall'=>1])->select();
    	$goodd=$goods->where(['classify'=>5,'isfall'=>1])->select();
    	$goode=$goods->where(['classify'=>6,'isfall'=>1])->select();
    	$goodf=$goods->where(['classify'=>7,'isfall'=>1])->select();
    	$goodg=$goods->where(['classify'=>8,'isfall'=>1])->select();
    	$goodaa=$goods->where(['classify'=>9,'isfall'=>1])->select();
    	$goodbb=$goods->where(['classify'=>11,'isfall'=>1])->select();
    	$data=$goods->where(['classify'=>10,'isfall'=>1])->select();
    	$this->assign('good',$good);
    	$this->assign('gooda',$gooda);
    	$this->assign('goodb',$goodb);
    	$this->assign('goodc',$goodc);
    	$this->assign('goodd',$goodd);
    	$this->assign('goode',$goode);
    	$this->assign('goodf',$goodf);
    	$this->assign('goodg',$goodg);
    	$this->assign('goodaa',$goodaa);
    	$this->assign('goodbb',$goodbb);
    	$this->assign('data',$data);
    	//dump($data);
	    return $this->fetch();
    }
    public function goodsxq($id)
    {
    	$uid=Session::get('uid');

    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
    	//$good=$goods->where(['classify'=>1])->select();
    	$good = Db::table('phone_goods')->where('gid='.$id)->select();
    	//dump($good);
    	$this->assign('good',$good);
    	$uid=$id;
    	$this->assign('id',$uid);

    	 return $this->fetch();
    }

	public function goodcar()
	{
		$uid=Session::get('uid');
		if(empty($uid)){
    		$this->success('未登录，请登陆',THINK_URLL);
    	}

    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
		$go = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->select();
		if(!empty($gnumber)){

		if($gnumber>=1){
		$mm=$go[0]['totalprice'];
		}
		//dump($mm);
		if($gnumber>=2){
		$mmm=$go[1]['totalprice'];
		$ccc=$mm+$mmm;
		}
		if($gnumber>=3){
		$mmmm=$go[2]['totalprice'];
		$cccc=$mmmm+$ccc;
		}
		if($gnumber>=4){
		$mmmmm=$go[3]['totalprice'];
		$ccccc=$mmmmm+$cccc;
		}
		if(!empty($ccccc)){
			$this->assign('price',$ccccc);
		} else if(!empty($cccc)){
			$this->assign('price',$cccc);
		} else if(!empty($ccc)){
			$this->assign('price',$ccc);
		} else if(!empty($mm)){
			$this->assign('price',$mm);
		}
			}
		$this->assign('go',$go);

		$request = Request::instance();
		//dump($good);

    	if(!empty($_POST))
    	{
    		$jr=$request->post('jr');
    	if($jr=='加入购物车')
    	{
    		$gid=$request->get('id');

    		//dump($gid);
    		$good = Db::table('phone_goods')->where(['gid'=>"$gid"])->select();
    		//dump($good);
    		if(!empty($good)){
    		$goodid=$good[0]['gid'];
    		$dd=Db::table('phone_inorder')->where(['userid'=>"$uid",'gid'=>"$goodid",'ddzt'=>'待支付'])->select();
    		//dump($dd);
    		if(!empty($dd)){
    		$goodnumber=$dd[0]['goodsnumber']+1;
    			}
			}
    	if(empty($dd)){

    		$goodsname=$good[0]['goodsname'];
    		$express=$good[0]['summary'];
    		$goodspic=$good[0]['public'];
    		$unitprice=$good[0]['price'];

    	//dump($address);

    	$address = Db::table('phone_user')->where('uid='.$uid)->value('addr');
    	if(empty($address)){
    		$this->success('请设置收货地址',THINK_URLB);
    		exit;
    	}
    	$data=['userid'=>"$uid",
    			'goodsname'=>"$goodsname",
    			'goodspic'=>"$goodspic",
    			'unitprice'=>"$unitprice",
    			'goodsnumber'=>1,
    			'address'=>"$address",
    			'express'=>"$express",
    			'gid'=>"$goodid",
    			'ddzt'=>'待支付',
    			'totalprice'=>"$unitprice",
    			];

    	Db::table('phone_inorder')->insert($data);
    			$this->success('加入购物车成功，前往购物车付款',THINK_URLA);
    			} else {
    				$aa=Db::table('phone_inorder')->where(['userid'=>"$uid",'gid'=>"$goodid"])->value('unitprice');
    				$jiage=$goodnumber*$aa;
    				Db::table('phone_inorder')->where(['userid'=>"$uid",'gid'=>"$goodid"])->update(['goodsnumber'=>"$goodnumber",'totalprice'=>"$jiage"]);
    				$this->success('购物车已经有此商品，请前往购物车付款',THINK_URLA);
    			}
    	  	}
       }
		return $this->fetch();
	}
	public function goodcara()
	{
		if(!empty($_POST)){
		$a=$_POST;
		$a=(array_keys($a,'删除'));
		$number=$a[0];
		Db::table('phone_inorder')->where(['orderid'=>"$number"])->delete();
		$this->success('删除订单成功',THINK_URLA);
		}

	}
	public function detail()
	{
		$uid=Session::get('uid');
    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
    	$data=Db::name('goods')->select();
    	//dump($data);
    	$this->assign('data',$data);
		return $this->fetch();
	}

}
